package com.gao.modules.userdepart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gao.base.Result;
import com.gao.modules.depart.pojo.SysDepart;
import com.gao.modules.depart.service.SysDepartService;
import com.gao.modules.user.mapper.SysUserMapper;
import com.gao.modules.user.vo.SysUserVo;
import com.gao.modules.userdepart.mapper.SysUserDepartMapper;
import com.gao.modules.userdepart.pojo.SysUserDepart;
import com.gao.modules.userdepart.service.SysUserDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SysUserDepartServiceImpl extends ServiceImpl<SysUserDepartMapper, SysUserDepart> implements SysUserDepartService {

    @Autowired
    private SysDepartService sysDepartService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public Result queryDepartAllMemberConut(Boolean allChildren, String departName) {
        SysDepart existDepart = sysDepartService.getOne(new QueryWrapper<SysDepart>().eq("depart_name", departName));
        if (allChildren){
            if (null == existDepart){
                return Result.error("该部门不存在，请检查部门名称");
            }
            Integer count = this.count(new QueryWrapper<SysUserDepart>().eq("depart_id", existDepart.getId()));
            String[] split = existDepart.getChildrenIds().split(",");
            List<String> childDepartIds = Arrays.asList(split);
            for (int i = 0; i < childDepartIds.size(); i++) {
//                全部子部门id都在children中 有时间改成递归格式
               count += this.count(new QueryWrapper<SysUserDepart>().eq("depart_id",childDepartIds.get(i)));
            }
            return Result.success(count);
        }else {
            if (null == existDepart)
                return Result.error("该部门不存在，请检查部门名称");
            return Result.success(this.count(new QueryWrapper<SysUserDepart>().eq("depart_id",existDepart.getId())));
        }
    }

    @Override
    public IPage<SysUserVo> queryDepartAllMemberDetail(Boolean allChildren, String departName, Page page) {
        SysDepart existDepart = sysDepartService.getOne(new QueryWrapper<SysDepart>().eq("depart_name", departName));
        if (null == existDepart){
            return null;
        }else {
            if (allChildren){
                String[] split = existDepart.getChildrenIds().split(",");
                List<String> childDepartIds = Arrays.asList(split);
                List<SysUserVo> allMembers = new ArrayList<>();
                for (int i = 0; i < childDepartIds.size(); i++) {
                    List<SysUserVo> singleDepart = sysUserMapper.queryDepartAllUserByIdBeList(childDepartIds.get(i));
                    allMembers.addAll(singleDepart);
                }
                //怎么把list转成Ipage丢回去？
                IPage<SysUserVo> iPage = new Page<>();
                page.setRecords(allMembers);
                page.setTotal(1);
                return iPage;
            }else{
                return sysUserMapper.queryDepartAllUser(page,departName);
            }
        }

    }
}
